package com.rules.admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.rules.admin.service.*;
import com.rules.admin.utils.Constant;
import com.rules.admin.entity.Project;
import com.rules.admin.entity.ProjectPermissions;
import com.rules.admin.entity.RulePackage;
import com.rules.admin.login.UserHolder;
import com.rules.admin.utils.ResultUtils;
import com.rules.admin.vo.RuleUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping({"/projects"})
@Controller
public class ProjectController {
   @Autowired
   private ProjectService projectService;
   @Autowired
   private RulePackageService rulePackageService;
   @Autowired
   private UnitCaseService unitCaseService;
   @Autowired
   private UserHolder<RuleUser> userHolder;
   @Autowired
   private UserGroupService userGroupService;
   @Autowired
   private PermissionService permissionService;
   @Autowired
   private RuleUserRoleService ruleUserRoleService;

   @RequestMapping(
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> addProject(@RequestBody Project project) {
      if (project.getId() == null) {
         this.projectService.save(project);
         return ResultUtils.getSuccessResultData(project);
      } else {
         this.projectService.updateById(project);
         List<RulePackage> rulePackages = this.rulePackageService.getRulePackagesByProjectId(project.getId());

         for(int i = 0; i < rulePackages.size(); ++i) {
            rulePackages.get(i).setGroupId(project.getProjectCode());
            this.rulePackageService.updateById(rulePackages.get(i));
         }

         return ResultUtils.getSuccessResultData(this.projectService.getById(project.getId()));
      }
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.DELETE}
   )
   @ResponseBody
   public Map<String, Object> deleteProject(@PathVariable("id") Long id) {
      Project project = this.projectService.getById(id);
      if (this.rulePackageService.getPackageByGroupId(project.getProjectCode()).size() == 0) {
         this.projectService.removeById(id);
         return ResultUtils.getSuccessResultData();
      } else {
         return ResultUtils.getFaildResultData(new String[]{"该项目下有规则包，请先删除规则包！"});
      }
   }

   @RequestMapping({"/deletes"})
   @ResponseBody
   public Map<String, Object> deleteProjects(List<Long> ids) {
      this.projectService.delete(ids);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.PUT}
   )
   @ResponseBody
   public Map<String, Object> updateRule(@RequestBody Project project) {
      this.projectService.update(project);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Project getRule(@PathVariable("id") Long id) {
      return this.projectService.getById(id);
   }

   @RequestMapping(
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public IPage<Project> queryPage(Project project, IPage<Project> page) {
      return this.projectService.queryPage(project, page);
   }

   @RequestMapping({"/allList"})
   @ResponseBody
   public Map<String, List> queryPage() {
      Map map = new HashMap();
      RuleUser ruleUser = (RuleUser)this.userHolder.getUser();
      if (ruleUser.equals(RuleUser.ANONYMOUS_USER)) {
         map.put("reload", true);
         return map;
      } else {
         if (this.ruleUserRoleService.getByUserId(ruleUser.getId()).getRoleId() == Constant.PERMISSION_ADMIN) {
            List<Project> projects = this.projectService.getAll();
            map.put("projects", projects);
         } else {
            map.put("projects", this.projectService.query());
         }

         return map;
      }
   }

   @RequestMapping({"/allParentList"})
   @ResponseBody
   public Map<String, List> getParentList() {
      Map map = new HashMap();
      RuleUser ruleUser = (RuleUser)this.userHolder.getUser();
      if (ruleUser.equals(RuleUser.ANONYMOUS_USER)) {
         map.put("reload", true);
         return map;
      } else {
         if (this.ruleUserRoleService.getByUserId(ruleUser.getId()).getRoleId() == Constant.PERMISSION_ADMIN) {
            List<Project> projects = this.projectService.getParentList();
            map.put("projects", projects);
         } else {
            map.put("projects", this.projectService.query());
         }

         return map;
      }
   }

   @RequestMapping({"/childList/{id}"})
   @ResponseBody
   public Map<String, List> getChildList(@PathVariable("id") String id) {
      Map map = new HashMap();
      RuleUser ruleUser = (RuleUser)this.userHolder.getUser();
      if (ruleUser.equals(RuleUser.ANONYMOUS_USER)) {
         map.put("reload", true);
         return map;
      } else {
         List<Project> projects = this.projectService.getChildList(id);
         map.put("projects", projects);
         return map;
      }
   }

   @RequestMapping(
      value = {"/id"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Map<String, Object> getName(Long packageId, Long projectId) {
      Map<String, Object> map = new HashMap();
      if (packageId != null) {
         map.put("packageName", this.rulePackageService.getById(packageId).getName());
      }

      map.put("projectName", this.projectService.getById(projectId).getProjectName());
      return ResultUtils.getSuccessResultData(map);
   }

   @RequestMapping(
      value = {"/arr"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public String[] getArrName(HttpServletRequest request) {
      //int l = false;
      RuleUser ruleUser = (RuleUser)this.userHolder.getUser();
      if (this.ruleUserRoleService.getByUserId(ruleUser.getId()).getRoleId() == Constant.PERMISSION_ADMIN) {
         List<Project> projects = this.projectService.getAll();
         String[] arr = new String[projects.size()];

         for(int i = 0; i < projects.size(); ++i) {
            arr[i] = ((Project)projects.get(i)).getProjectName();
         }

         return arr;
      } else {
         List ownerIds = Lists.newArrayList();
         ownerIds.add(ruleUser.getUserId());
         List<Long> groupIds = userGroupService.getGroupIds(ruleUser.getUserId());
         if (groupIds.size() != 0) {
            ownerIds.addAll(groupIds);
         }

         List<ProjectPermissions> list = this.permissionService.selectProjectOfPermission(ownerIds);
         return (String[])list.stream().map(ProjectPermissions::getProjectName).distinct().toArray((x$0) -> {
            return new String[x$0];
         });
      }
   }

   @RequestMapping(
      value = {"/ids"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Map<String, Object> getProjectIdAndPackageId(String artifactId, String groupId) {
      Map<String, Object> map = new HashMap();
      Project project = this.projectService.getByProjectCode(groupId);
      map.put("projectId", project.getId().toString());
      RulePackage rulePackage = this.rulePackageService.getByGroupId(artifactId);
      map.put("packageId", rulePackage.getId().toString());
      return map;
   }
}
